/**
 * Copyright (c) 2007 lyxite.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
using Weys.Lib;

namespace Weys.SkinComposer
{
	/// <summary>
	/// a tools to create a skin with little effort
	/// </summary>
	public class SkinComposer : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;

		public enum Actions
		{
			None,
			DrawingSkinRect,
			DrawingShotRect,
			SelectingColor
		};
		private Skin skin = null;
		private Rectangle skinRect = new Rectangle(0,0,0,0);
		private Rectangle shotRect = new Rectangle(0,0,0,0);
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItemNew;
		private System.Windows.Forms.MenuItem menuItemOpen;
		private System.Windows.Forms.MenuItem menuItemSave;
		private System.Windows.Forms.MenuItem menuItemSaveAs;
		private System.Windows.Forms.MenuItem menuItemExit;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.MenuItem menuItem10;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItemSkinProperties;
		private Actions action = Actions.None;

		public SkinComposer()
		{
		}
		
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItemNew = new System.Windows.Forms.MenuItem();
			this.menuItemOpen = new System.Windows.Forms.MenuItem();
			this.menuItemSave = new System.Windows.Forms.MenuItem();
			this.menuItemSaveAs = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.menuItem10 = new System.Windows.Forms.MenuItem();
			this.menuItemSkinProperties = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItemExit = new System.Windows.Forms.MenuItem();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.tabControl1.SuspendLayout();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemNew,
																					  this.menuItemOpen,
																					  this.menuItemSave,
																					  this.menuItemSaveAs,
																					  this.menuItem7,
																					  this.menuItem9,
																					  this.menuItem10,
																					  this.menuItemSkinProperties,
																					  this.menuItem2,
																					  this.menuItemExit});
			this.menuItem1.Text = "&File";
			// 
			// menuItemNew
			// 
			this.menuItemNew.Index = 0;
			this.menuItemNew.Text = "&New";
			this.menuItemNew.Click += new System.EventHandler(this.menuItemNew_Click);
			// 
			// menuItemOpen
			// 
			this.menuItemOpen.Index = 1;
			this.menuItemOpen.Text = "&Open";
			this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
			// 
			// menuItemSave
			// 
			this.menuItemSave.Index = 2;
			this.menuItemSave.Text = "&Save";
			this.menuItemSave.Click += new System.EventHandler(this.menuItemSave_Click);
			// 
			// menuItemSaveAs
			// 
			this.menuItemSaveAs.Index = 3;
			this.menuItemSaveAs.Text = "&Save As ...";
			this.menuItemSaveAs.Click += new System.EventHandler(this.menuItemSaveAs_Click);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 4;
			this.menuItem7.Text = "-";
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 5;
			this.menuItem9.Text = "&Import Picture";
			// 
			// menuItem10
			// 
			this.menuItem10.Index = 6;
			this.menuItem10.Text = "-";
			// 
			// menuItemSkinProperties
			// 
			this.menuItemSkinProperties.Index = 7;
			this.menuItemSkinProperties.Text = "Skin P&roperties";
			this.menuItemSkinProperties.Click += new System.EventHandler(this.menuItemSkinProperties_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 8;
			this.menuItem2.Text = "-";
			// 
			// menuItemExit
			// 
			this.menuItemExit.Index = 9;
			this.menuItemExit.Text = "E&xit";
			this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Location = new System.Drawing.Point(32, 64);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.TabIndex = 0;
			// 
			// tabPage1
			// 
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(192, 74);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "tabPage1";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(104, 16);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(16, 24);
			this.button2.Name = "button2";
			this.button2.TabIndex = 2;
			this.button2.Text = "button2";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Weys.SkinComposer
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(208, 102);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.tabControl1);
			this.Menu = this.mainMenu1;
			this.Name = "Weys.SkinComposer";
			this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SkinComposer_MouseDown);
			this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.SkinComposer_MouseUp);
			this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.SkinComposer_MouseMove);
			this.tabControl1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.Run(new SkinComposer());
		}

		private void NewSkin(string name, int modeCount, Bitmap [] images)
		{
			skin = new Skin();
			skin.Name = name;
			skin.IsImageShared = false;
			skin.IsTransparencyEnabled = false;
			skin.ModeCount = modeCount;
			skin.Mode = 0;
			skin.TransparencyColor = Color.White;
			skin.Pieces = new SkinPiece[modeCount];
			for(int i = 0; i < modeCount; i++)
			{
				skin.Pieces[i] = new SkinPiece();
				skin.Pieces[i].Name = "";
				skin.Pieces[i].SkinImage = images[i];
				skin.Pieces[i].SkinSize = images[i].Size;
			}
			//skin.ActiveSkin.SkinImage
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			Graphics g = e.Graphics;			
			g.DrawImage(skin.ActiveSkin.SkinImage,0,0);
			g.DrawRectangle(Pens.Red, shotRect);
			g.DrawRectangle(Pens.Blue, skinRect);
			//base.OnPaint (e);
		}
		private void SaveSkin()
		{
			FileDialog fd = new SaveFileDialog();
			fd.Filter = "*.XML (Skin Files)|*.xml";
			DialogResult dr = fd.ShowDialog();
			if(dr == DialogResult.OK)
				skin.Save(fd.FileName);
		}
		private void OpenSkin()
		{
			FileDialog fd = new OpenFileDialog();
			fd.Filter = "*.XML (Skin Files)|*.xml";
			DialogResult dr = fd.ShowDialog();
			if(dr == DialogResult.OK)
			{
				try
				{
					skin = Skin.Load(fd.FileName);
				}
				catch
				{
					MessageBox.Show(this, "Invalid skin file");
				}
			}
		}

		private void SkinComposer_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(e.Button == MouseButtons.Left)
			{
				if(action == Actions.DrawingShotRect)
				{
					shotRect.X = e.X;
					shotRect.Y = e.Y;
					Refresh();
				}
				else if(action == Actions.DrawingSkinRect)
				{
					skinRect.X = e.X;
					skinRect.Y = e.Y;
					Refresh();
				}
				else if(action == Actions.SelectingColor)
				{
					skin.TransparencyColor = GetColorAt(e.X, e.Y);
					this.Cursor = Cursors.Arrow;
					action = Actions.None;
				}
			}
		}
		private Color GetColorAt(int x, int y)
		{
			return skin.ActiveSkin.SkinImage.GetPixel(x, y);
		}
		private void SkinComposer_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(e.Button == MouseButtons.Left)
			{
				if(action == Actions.DrawingShotRect)
				{
					shotRect.Width = e.X - shotRect.X;
					shotRect.Height = e.Y - shotRect.Y;
					Refresh();
				}
				else if(action == Actions.DrawingSkinRect)
				{
					skinRect.Width = e.X - skinRect.X;
					skinRect.Height = e.Y - skinRect.Y;
					Refresh();
				}
			}
		}

		private void SkinComposer_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(e.Button == MouseButtons.Left)
				action = Actions.None;
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			FileDialog fd = new OpenFileDialog();
			fd.Filter = "*.Inf (Skin Files)|*.inf";
			DialogResult dr = fd.ShowDialog();
			if(dr == DialogResult.OK)
			{
				ConvertInfToXml(fd.FileName);
			}
		}
		private void ConvertInfToXml(string f)
		{
			Skin skin = new Skin();
			string path = f.Substring(0,f.LastIndexOf("\\")+1);
			skin.Name = f.Substring(path.Length,f.Length - path.Length - 4);
			skin.Mode = 0;
			skin.ModeCount = 1;
			skin.IsImageShared = false;
			skin.Pieces = new SkinPiece[1];
			skin.Pieces[0] = new SkinPiece();
			skin.ActiveSkin.Name = "";
				
			StreamReader sr = new StreamReader(f);
				
			string s = sr.ReadLine();
			if(s.Trim() == "") s = sr.ReadLine();
			s = s.Substring(8);
			skin.ActiveSkin.SkinImage = new Bitmap(path+s);
			s.Trim();
				
			s = sr.ReadLine();
			if(s.Trim() == "") s = sr.ReadLine();
			s = s.Substring(s.IndexOf("(")+1);
			string x,y,w,h;
			s = s.Trim();
			s = s.Substring(0, s.Length - 1);
			x = s.Substring(0, s.IndexOf(",") );
			s = s.Substring(s.IndexOf(",")+1);
			y = s.Substring(0, s.IndexOf(",") );
			s = s.Substring(s.IndexOf(",")+1);
			w = s.Substring(0, s.IndexOf(",") );
			h = s.Substring(s.IndexOf(",")+1);

			skin.ActiveSkin.CaptureAreaOffset  = new Point (int.Parse(x.Trim()), int.Parse(y.Trim()));
			skin.ActiveSkin.CaptureAreaSize = new Size (int.Parse(w.Trim()), int.Parse(h.Trim()));
				
			skin.TransparencyColor = ((Bitmap)skin.ActiveSkin.SkinImage).GetPixel(0,0);
			skin.IsTransparencyEnabled = true;

			skin.Save(f.Substring(0, f.Length - 4) + ".xml");	
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			
		}

		private void menuItemNew_Click(object sender, System.EventArgs e)
		{
			SkinDialog sd = new SkinDialog(null);
			if(sd.ShowDialog(this) == DialogResult.OK)
			{
				skin = sd.Skin;
			}
		}

		private void menuItemOpen_Click(object sender, System.EventArgs e)
		{
			FileDialog fd = new OpenFileDialog();
			fd.Filter = "*.xml (Skin Files)|*.xml";
			if(fd.ShowDialog() == DialogResult.OK)
			{
				skin = Skin.Load(fd.FileName);
			}
		}

		private void menuItemSave_Click(object sender, System.EventArgs e)
		{
			
		}

		private void menuItemSaveAs_Click(object sender, System.EventArgs e)
		{
			FileDialog fd = new SaveFileDialog();
			fd.Filter = "*.xml (Skin Files)|*.xml";
			if(fd.ShowDialog() == DialogResult.OK)
			{
				if(skin != null)
					skin.Save(fd.FileName);
			}		
		}

		private void menuItemExit_Click(object sender, System.EventArgs e)
		{
			this.Close();	
		}
		private void menuItemSkinProperties_Click(object sender, System.EventArgs e)
		{
			if(skin == null) return;
			SkinDialog sd = new SkinDialog(skin);
			if(sd.ShowDialog(this) == DialogResult.OK)
			{
				skin = sd.Skin;
			}
		}
	}
}
